<?php

namespace app\core;
use Throwable;
class Log
{

    private static $path = LOG;
    
    public static function info(mixed $msg)
    {
        $fileName = self::getDate() . '.info.log';
        self::record($fileName, $msg);
    }
    public static function error(mixed $msg)
    {
        // ccc
        $fileName = self::getDate() . '.error.log';
        self::record($fileName, $msg);
    }

    private static function getDate()
    {
        return self::$path . date('Ymd');
    }
    public static function exceptionError(Throwable $th){
        $fileName = self::getDate() . '.exeptionError.log';
        self::record($fileName, ['file' => $th->getFile(), 'line' => $th->getLine(), 'message' => $th->getMessage()]);
    }
    public static function dd(){
        
    }
    private static function record($fileName, mixed $msg)
    {
        $source=File::open($fileName);
        $content='[' . date('Y-m-d H:i:s') . ']' . json_encode($msg,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . "\r\n";
        File::write($source,$content);
        File::close($source);
        // error_log( '[' . date('Y-m-d H:i:s') . ']' . json_encode($msg,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . "\r\n", 3,$fileName);
    }

    public static function system_log($msg)
    {
        $fileName = self::getDate() . '.system.log';
        error_log('[' . date('Y-m-d H:i:s') . ']' . $msg . "\r\n", 3,$fileName);
    }
}
